﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace QUANLYDIEM
{
    class Database
    {
        public static string ckn = "";
        //public static string ckn=@"Data Source=.\SQLEXPRESS;AttachDbFilename="+Application.StartupPath+@"\DB\QLXE.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
        //public static string ckn = @"Data Source=LENOVO\SQL1;Initial Catalog=QLXE;Integrated Security=True";
        public static SqlConnection ket_noi = null;
        public static SqlCommand cmd;

        public static void Tao_ket_noi()
        {

            if (ket_noi == null)
                ket_noi = new SqlConnection(ckn);
        }
        public static void Tao_ket_noi(String kn)
        {
            ckn = kn;
            if (ket_noi == null)
                ket_noi = new SqlConnection(ckn);
        }
        public static void CreateCommand(String command, CommandType type)
        {
            Tao_ket_noi();
            cmd = new SqlCommand();
            cmd.Connection = ket_noi;
            cmd.CommandText = command;
            cmd.CommandType = type;
        }
        public static void CreateCommand(String command)
        {
            cmd = new SqlCommand();
            cmd.Connection = ket_noi;
            cmd.CommandText = command;
            cmd.CommandType = CommandType.Text;
        }
        public static void AddParameter(string name, DbType type, object value, ParameterDirection direction)
        {
            SqlParameter para = new SqlParameter();
            para.ParameterName = name;
            para.DbType = type;
            para.Value = value;
            para.Direction = direction;
            cmd.Parameters.Add(para);

        }
        public static void AddParameter(string name, DbType type, object value)
        {
            SqlParameter para = new SqlParameter();
            para.ParameterName = name;
            para.DbType = type;
            para.Value = value;
            para.Direction = ParameterDirection.Input;
            cmd.Parameters.Add(para);

        }
        public static void AddParameter(string name, SqlDbType type, object value)
        {
            SqlParameter para = new SqlParameter();
            para.ParameterName = name;
            para.SqlDbType = type;
            para.Value = value;
            para.Direction = ParameterDirection.Input;
            cmd.Parameters.Add(para);

        }
        public static int ExecuteNonQuery()
        {
            if (cmd.Connection.State == ConnectionState.Closed) cmd.Connection.Open();
            // if (cmd.Connection.State == ConnectionState.Closed) cmd.Connection.Open();
            return cmd.ExecuteNonQuery();
        }
        public static SqlDataReader ExecuteReader()
        {
            if (cmd.Connection.State == ConnectionState.Closed) cmd.Connection.Open();
            // if (cmd.Connection.State == ConnectionState.Closed) cmd.Connection.Open();
            return cmd.ExecuteReader();
        }
        public static System.Data.DataTable ExecuteTable()
        {
            if (ket_noi.State == ConnectionState.Open) ket_noi.Close();
            if (cmd.Connection.State == ConnectionState.Closed) cmd.Connection.Open();
            System.Data.DataTable dt = new System.Data.DataTable();
            // if (cmd.Connection.State == ConnectionState.Closed) cmd.Connection.Open();
            //SqlDataReader reader = ExecuteReader();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(dt);
            return dt;
        }
    }
}
